package ${package.Controller};

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.inter.fsb.entity.${entity};
import org.inter.fsb.service.${table.serviceName};
import org.inter.fsb.utils.ProxyResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
    * ${table.comment!}
    * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Api(tags = "${cfg.describe}")
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("/inter/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
    <#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
    <#else>
public class ${table.controllerName} {
    </#if>

    @Autowired
    private ${table.serviceName} service;

    @ApiOperation("查询一个${cfg.myTitle}")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public @ResponseBody
    ProxyResponse get(${entity} entity) {
    Wrapper<${entity}> wrapper = new QueryWrapper<${entity}>(entity);
    ${entity} result = service.getOne(wrapper);
    return toJson(result);
    }

    @ApiOperation("查询${cfg.myTitle}")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public @ResponseBody
    ProxyResponse list(${entity} entity) {
    Wrapper<${entity}> wrapper = new QueryWrapper<${entity}>(entity);
    List<${entity}> result = service.list(wrapper);
    return toJson(result);
    }

    @ApiOperation("分页查询${cfg.myTitle}")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public @ResponseBody
    ProxyResponse page(${entity} entity,
                              @RequestParam(name = "page",required = false, defaultValue = "1") Integer page,
                              @RequestParam(name = "size",required = false, defaultValue = "10") Integer size) {

        Page<${entity}> pageable = new Page<>(page,size);
        Wrapper<${entity}> wrapper = new QueryWrapper<${entity}>(entity);
        Page<${entity}> result = service.page(pageable,wrapper);
        return toJson(result);
    }

     @ApiOperation("根据ID查询功能")
     @RequestMapping(value = "/find", method = RequestMethod.GET)
     public @ResponseBody
     ProxyResponse findOne(${entity} entity){
         Wrapper<${entity}> wrapper = new QueryWrapper<${entity}>(entity);
         ${entity} result = service.getOne(wrapper);
         return toJson(result);
     }


    @ApiOperation("新建或更新数据${cfg.myTitle}")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public @ResponseBody ProxyResponse save(@RequestBody ${entity} entity) throws Exception{
        boolean result = service.saveOrUpdate(entity);
        if (result) {
            return toJson("更新成功");
        }
        throw new Exception("更新失败");
    }

    @ApiOperation("删除数据${cfg.myTitle}")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public @ResponseBody ProxyResponse remove(@RequestParam("id")int id) throws Exception{
        boolean result = service.removeById(id);
        if (result) {
            return toJson("更新成功");
        }
        throw new Exception("删除失败");
    }

}
</#if>